import { getStream } from '../Core/connect'
import * as MathUtils from '../Math/MathUtils'

export const loadTowerByJson = {
    // 根据json加载塔实体
    autoLoadTower(path:string) {
        console.log(2222)
        const stream = getStream()
        const str = {instancedFunction: 'autoLoadTower',path:path}
        stream.emitUIInteraction(str)
    },
    
    // 根据塔坐标和角度获取塔的连接点位置
    getTowerLine(path:string) {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const str = {instancedFunction: 'getTowerLine',sessionId:sessionId,path:path}
            stream.emitUIInteraction(str)
            const getTowerLine = (data: any) => {
                const returnData = JSON.parse(data)
                if(returnData.sessionId === sessionId){
                    let lists:any = []
                    returnData.data.forEach((val:any) => {
                        const returnData2 = JSON.parse(val)
                        lists.push(returnData2)
                        console.log(lists)
                        resolve(lists)
                    });
                }
            }
            stream.addResponseEventListener(`getTowerLine`, getTowerLine)
        })
    },
    
    // 根据塔坐标和角度获取塔的连接点位置
    getLineByJson(path:string,lineLinkPath:string,offsetPath:string) {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const str = {instancedFunction: 'getLineByJson',sessionId:sessionId,path:path,lineLinkPath:lineLinkPath,offsetPath:offsetPath}
            stream.emitUIInteraction(str)
            const getLineByJson = (data: any) => {
                const returnData = JSON.parse(data)
                console.log('读取完成')
                if(returnData.sessionId === sessionId){
                    let lists:any = []
                    returnData.data.forEach((val:any) => {
                        const returnData2 = JSON.parse(val)
                        lists.push(returnData2)
                        console.log(lists)
                        resolve(lists)
                    });
                }
            }
            stream.addResponseEventListener(`getLineByJson`, getLineByJson)
        })
    }
}